<template>
  <view class="navbar">
    <view class="navbar-fixed">
      <!-- 状态栏小程序撑起高度 -->
      <view :style="{height:statusBarHeight+'px'}"></view>
      <view class="navbar-content" :style="{height:navBarHeight+'px',width:windowWidth+'px'}">
        <view class="navbar-search">
          <view class="navbar-search_icon">
            <uni-icons type="search" size="16" color="#999"></uni-icons>
          </view>
          <view class="navbar-serach">
            <input class="navbar-search_text" type="text" v-model="val" placeholder="输入区域、容量和预计年收入搜索" />
          </view>
        </view>
      </view>
    </view>
    <!-- 需要添加占位符高度  状态栏高度+导航栏高度（否则下面tab会塌陷）-->
    <view :style="{height: statusBarHeight+navBarHeight+'px'}"></view>
    <view>正文</view>
    <uni-popup ref="registerPopup" type="center">
<!--      <span style="font-size:14px;">-->
<!--        <button class="registerPopup_register" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">微信用户快捷登录</button>-->
<!--      允许授权获取手机号</span>-->
      <view class='registerPopup'>
        <div class="registerPopup_title">微信用户快捷登录</div>
        <view>
          <button  @click='cancle' class="registerPopup_cancle">取消</button>
          <button class="registerPopup_register"  open-type='getPhoneNumber' @getphonenumber="getPhoneNumber">确认</button>
<!--          <button class="registerPopup_register"  open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber">确认</button>-->
        </view>
      </view>
    </uni-popup>
  </view>

</template>

<script>
import Http from "@/libs/Http";
import wxPromisify from "@/libs/wxPromisify";

export default {
  name: 'navbar',
  data() {
    return {
      statusBarHeight: 20,/* 状态栏高度 */
      navBarHeight: 45,/* 导航栏高度 */
      windowWidth: 375,/* 窗口宽度 */
      /* 设定状态栏默认高度 */
      val: '',/* 导航栏搜索框的值 */
      encrypdata: '',
      ivdata: '',
      sessionkey: ''
    };
  },
  created() {
    // 获取手机系统信息
    const info = uni.getSystemInfoSync()
    // 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
    this.statusBarHeight = info.statusBarHeight
    this.windowWidth = info.windowWidth
    // 除了h5 app mp-alipay的情况下执行
    // #ifndef H5 || APP-PLUS || MP-ALIPAY
    // 获取胶囊的位置
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
    console.log(menuButtonInfo);
    // (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
    this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
    this.windowWidth = menuButtonInfo.left
    // #endif
  },
  onLoad() {
    this.login();
    this.$refs.registerPopup.open();
  },
  methods: {
    cancle(){
      this.$refs.registerPopup.close();
    },
    async login() {
      try {
        let loginRes = await wxPromisify.login();
        console.log('loginRes',loginRes);
        let code = loginRes.code;
        if (code) {
          //发起网络请求
          let code2Session = await Http.get('https://www.zhangyifansjdd.top/api/wx/code2Session', {code})
          console.log('code2Session', code2Session);
          let userInfo =await wxPromisify.getUserInfo();
          console.log('userInfo',userInfo);
        }
      } catch (e) {
        console.log(e);
      }
    },
    async getPhoneNumber(res) {
      this.$refs.registerPopup.close();
      console.log('getPhoneNumber',res);
      let code = res.detail.code;
      let getAccessToken = await Http.get('http://localhost:8080/api/wx/getAccessToken');
      console.log(getAccessToken.access_token);
      let getPhoneNumber  = await Http.get('http://localhost:8080/api/wx/getPhoneNumber',{
        code,
        access_token:getAccessToken.access_token
      });
      console.log(getPhoneNumber);
    },

  }
}
</script>

<style lang="scss">
@import './../../uni.scss';

.navbar {
  .navbar-fixed {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99;
    width: 100%;
    background-color:#f07373;

    .navbar-content {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0 15px;
      height: 45px;
      box-sizing: border-box;

      .navbar-search {
        display: flex;
        align-items: center;
        padding: 0 10px;
        width: 100%;
        height: 30px;
        border-radius: 30px;
        background-color: #fff;

        .navbar-search_icon {
          // width: 10px;
          // height: 10px;
          margin-right: 10px;
        }

        .navbar-search_text {
          width: 100%;
          font-size: 12px;
          color: #999;
        }
      }

      &.search {
        padding-left: 0;

        .navbar-content__search-icons {
          margin-left: 10px;
          margin-right: 10px;
        }

        .navbar-search {
          border-radius: 5px;
        }
      }
    }

  }
  //.floatContent {
  //  /* padding: 20rpx 0;   */
  //  width: 80%;
  //  background: #fff;
  //  margin: 40% auto;
  //  border-radius: 20px;
  //  display: flex;
  //  flex-direction: column;
  //  justify-content: space-around;
  //  align-items: center;
  //  position: relative;
  //  height: 255px;
  //}
  //.floatContent text {
  //  color: #000;
  //  font-size: 40px;
  //  display: block;
  //  margin: 0 auto;
  //  position: absolute;
  //  top: 50px;
  //  /* text-align: center;   */
  //  /* line-height: 60rpx;   */
  //  border-radius: 30px;
  //}
  //.floatText{
  //  width: 100%;
  //  height: 100px;
  //  display: flex;
  //  justify-content: flex-start;
  //  position: absolute;
  //  bottom: 0;
  //}
  //.btn-cancle{
  //  line-height: 100px;
  //  flex: 1;
  //  margin: 0;
  //  padding: 0;
  //  border-radius: 0;
  //}

  .registerPopup {
    font-size: 0;
    min-width: 80%;
    min-height: 80%;
    background: white;
    border-radius: 10px;
    padding: 10px;
  }
  .registerPopup_title {
    font-size: 16px;
    font-weight: 400;
    margin-bottom: 20px;
  }
  .registerPopup_cancle{
    font-size: 12px;
    font-weight: 80;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: white;
    border: 0;
    color: black;
  }
  .registerPopup_register {
    font-size: 12px;
    font-weight: 80;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: white;
    border: 0;
    color: deepskyblue;
    margin-left: 10px;
    //height: 30px;
    //margin-bottom: 10px;
  }

}
</style>